<template>
  <div class="login-container">
    <div class="login-header">
      <h1>智慧农业助手</h1>
      <p>走进智慧农业，创建幸福生活</p>
    </div>
    
    <div class="login-tabs">
      <div 
        :class="['login-tab', { active: isPasswordLogin }]" 
        @click="isPasswordLogin = true"
      >
        账号密码登录
      </div>
      <div 
        :class="['login-tab', { active: !isPasswordLogin }]" 
        @click="isPasswordLogin = false"
      >
        手机验证码登录
      </div>
    </div>
    
    <div class="login-form">
      <div class="form-group">
        <input 
          v-model="usernameOrPhone" 
          type="text" 
          placeholder="请输入用户名/手机号" 
          @blur="validateUsernameOrPhone"
        >
        <span v-if="usernameOrPhoneError" class="error-message">{{ usernameOrPhoneError }}</span>
      </div>
      
      <div class="form-group" v-if="isPasswordLogin">
        <input 
          v-model="password" 
          type="password" 
          placeholder="请输入密码" 
          @blur="validatePassword"
        >
        <span v-if="passwordError" class="error-message">{{ passwordError }}</span>
      </div>
      
      <div class="form-group" v-else>
        <div class="verification-code">
          <input 
            v-model="verificationCode" 
            type="text" 
            placeholder="请输入验证码" 
            @blur="validateVerificationCode"
          >
          <button 
            :disabled="isSendingCode || codeCountdown > 0" 
            @click="sendVerificationCode"
          >
            {{ codeCountdown > 0 ? `${codeCountdown}秒后重发` : '获取验证码' }}
          </button>
        </div>
        <span v-if="verificationCodeError" class="error-message">{{ verificationCodeError }}</span>
      </div>
      
      <button 
        class="login-button" 
        @click="handleLogin"
        :disabled="isLoggingIn"
      >
        {{ isLoggingIn ? '登录中...' : '登录' }}
      </button>
    </div>
    
    <div class="third-party-login">
      <p>第三方登录</p>
      <div class="social-icons">
        <button class="social-icon wechat" @click="thirdPartyLogin('wechat')">
          <i class="fab fa-weixin"></i>
        </button>
        <button class="social-icon qq" @click="thirdPartyLogin('qq')">
          <i class="fab fa-qq"></i>
        </button>
        <button class="social-icon weibo" @click="thirdPartyLogin('weibo')">
          <i class="fab fa-weibo"></i>
        </button>
      </div>
    </div>
    
    <div class="login-footer">
      <router-link to="/register">没有账号？手机号快捷登录</router-link>
    </div>
  </div>
</template>

<script>
import { ref } from 'vue';
import { useRouter } from 'vue-router';

export default {
  setup() {
    const router = useRouter();
    
    // 登录方式切换
    const isPasswordLogin = ref(true);
    
    // 表单数据
    const usernameOrPhone = ref('');
    const password = ref('');
    const verificationCode = ref('');
    
    // 验证相关
    const usernameOrPhoneError = ref('');
    const passwordError = ref('');
    const verificationCodeError = ref('');
    
    // 验证码相关
    const codeCountdown = ref(0);
    const isSendingCode = ref(false);
    
    // 登录状态
    const isLoggingIn = ref(false);
    
    // 验证用户名/手机号
    const validateUsernameOrPhone = () => {
      if (!usernameOrPhone.value) {
        usernameOrPhoneError.value = '请输入用户名或手机号';
        return false;
      }
      
      // 简单手机号验证
      if (!/^[1][3-9][0-9]{9}$/.test(usernameOrPhone.value)) {
        usernameOrPhoneError.value = '请输入有效的手机号';
        return false;
      }
      
      usernameOrPhoneError.value = '';
      return true;
    };
    
    // 验证密码
    const validatePassword = () => {
      if (!password.value) {
        passwordError.value = '请输入密码';
        return false;
      }
      
      if (password.value.length < 6) {
        passwordError.value = '密码长度至少为6位';
        return false;
      }
      
      passwordError.value = '';
      return true;
    };
    
    // 验证验证码
    const validateVerificationCode = () => {
      if (!verificationCode.value) {
        verificationCodeError.value = '请输入验证码';
        return false;
      }
      
      if (verificationCode.value.length !== 6) {
        verificationCodeError.value = '验证码长度为6位';
        return false;
      }
      
      verificationCodeError.value = '';
      return true;
    };
    
    // 发送验证码
    const sendVerificationCode = () => {
      if (!validateUsernameOrPhone()) return;
      
      isSendingCode.value = true;
      
      // 模拟发送验证码
      setTimeout(() => {
        isSendingCode.value = false;
        
        // 设置60秒倒计时
        codeCountdown.value = 60;
        const timer = setInterval(() => {
          codeCountdown.value--;
          if (codeCountdown.value <= 0) {
            clearInterval(timer);
          }
        }, 1000);
        
        // 这里可以添加实际发送验证码的API调用
      }, 1000);
    };
    
    // 处理登录
    const handleLogin = () => {
      if (isLoggingIn.value) return;
      
      // 验证表单
      let isValid = validateUsernameOrPhone();
      
      if (isPasswordLogin.value) {
        isValid = isValid && validatePassword();
      } else {
        isValid = isValid && validateVerificationCode();
      }
      
      if (!isValid) return;
      
      isLoggingIn.value = true;
      
      // 模拟登录请求
      setTimeout(() => {
        // 这里可以添加实际登录的API调用
        router.push('/dashboard');
        isLoggingIn.value = false;
      }, 1500);
    };
    
    // 第三方登录
    const thirdPartyLogin = (platform) => {
      // 这里可以添加第三方登录的逻辑
      console.log(`使用${platform}登录`);
    };
    
    return {
      isPasswordLogin,
      usernameOrPhone,
      password,
      verificationCode,
      usernameOrPhoneError,
      passwordError,
      verificationCodeError,
      codeCountdown,
      isSendingCode,
      isLoggingIn,
      validateUsernameOrPhone,
      validatePassword,
      validateVerificationCode,
      sendVerificationCode,
      handleLogin,
      thirdPartyLogin
    };
  }
}
</script>

<style scoped>
.login-container {
  max-width: 400px;
  margin: 0 auto;
  padding: 20px;
  background-color: white;
  border-radius: 8px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}

.login-header {
  text-align: center;
  margin-bottom: 20px;
}

.login-header h1 {
  font-size: 24px;
  color: #2c3e50;
  margin-bottom: 5px;
}

.login-header p {
  color: #7f8c8d;
  font-size: 14px;
}

.login-tabs {
  display: flex;
  margin-bottom: 20px;
  border-bottom: 1px solid #e0e0e0;
}

.login-tab {
  flex: 1;
  padding: 10px;
  text-align: center;
  cursor: pointer;
  transition: all 0.3s;
}

.login-tab.active {
  color: #4CAF50;
  border-bottom: 2px solid #4CAF50;
  font-weight: bold;
}

.form-group {
  margin-bottom: 15px;
}

input[type="text"],
input[type="password"] {
  width: 100%;
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 4px;
  box-sizing: border-box;
}

.verification-code {
  display: flex;
}

.verification-code input {
  flex: 1;
  margin-right: 10px;
}

.verification-code button {
  padding: 10px;
  background-color: #4CAF50;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

.verification-code button:disabled {
  background-color: #9e9e9e;
  cursor: not-allowed;
}

.error-message {
  color: #f44336;
  font-size: 12px;
  margin-top: 5px;
}

.login-button {
  width: 100%;
  padding: 12px;
  background-color: #4CAF50;
  color: white;
  border: none;
  border-radius: 4px;
  font-size: 16px;
  cursor: pointer;
  transition: background-color 0.3s;
}

.login-button:hover {
  background-color: #45a049;
}

.login-button:disabled {
  background-color: #9e9e9e;
  cursor: not-allowed;
}

.third-party-login {
  margin-top: 20px;
  text-align: center;
}

.third-party-login p {
  margin-bottom: 10px;
  color: #7f8c8d;
}

.social-icons {
  display: flex;
  justify-content: center;
  gap: 15px;
}

.social-icon {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  font-size: 20px;
  color: white;
}

.wechat {
  background-color: #07C160;
}

.qq {
  background-color: #12B7F5;
}

.weibo {
  background-color: #E6162D;
}

.login-footer {
  text-align: center;
  margin-top: 20px;
}

.login-footer a {
  color: #4CAF50;
  text-decoration: none;
}

.login-footer a:hover {
  text-decoration: underline;
}
</style>